package main

import (
	"fmt"
	"log"

	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

type Person struct {
	Name  string
	Phone string
}

func main() {
	// connect mongodb
	session, err := mgo.Dial("127.0.0.1:27020")
	if err != nil {
		panic(err)
	}
	defer session.Close()

	// select db and collections
	c := session.DB("shansjlin").C("linyanwen")

	// insert data
	err = c.Insert(&Person{"shansjlin", "13539819575"},
		&Person{"linyanwen", "15884572989"})
	if err != nil {
		log.Fatal(err)
	}

	// find data
	result := Person{}
	err = c.Find(bson.M{"name": "shansjlin"}).One(&result)
	if err != nil {
		log.Fatal(err)
		panic(err)
	}

	fmt.Println("Phone:", result.Phone)
}
